package com.wenhai.spring.aop.after;

import com.wenhai.spring.aop.Guiatrist;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.lang.NonNull;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * {@link org.springframework.aop.AfterReturningAdvice} 示例
 *
 * @author wenhai
 * @date   2021/5/5
 * @see org.springframework.aop.AfterReturningAdvice
 */
public class SimpleAfterReturningAdvice implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object returnValue, @NonNull Method method, @NonNull Object[] args, Object target) throws Throwable {
        System.out.printf("returnValue:%s, Method: %s, Args:%s, target:%s\n", returnValue, method.getName(), Arrays.toString(args), target.getClass().getSimpleName());
    }

    public static void main(String[] args) {
        Guiatrist guiatrist = new Guiatrist();
        ProxyFactory pf = new ProxyFactory();
        pf.addAdvice(new SimpleAfterReturningAdvice());
        pf.setTarget(guiatrist);
        Guiatrist proxy = (Guiatrist) pf.getProxy();
        proxy.sing("文海");
    }
}
